﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using DataAccess;
using System.Net.Mail;

namespace KhoaLuan.Account
{
    public partial class ForgotPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string SendMailReset(string username, string email)
        {
            try
            {
                tbUsers user = null;
                tbUsers_Proxy proxy = new tbUsers_Proxy();
                if (!string.IsNullOrWhiteSpace(username))
                {
                    user = proxy.GetByUserName_tbUsers_Proxy(username);
                }
                else
                {
                    user = proxy.GetByEmail_tbUsers_Proxy(email);
                }
                if (user != null)
                {
                    var emailreset = user.Email;
                    var deadlinereset = DateTime.Now.AddMinutes(10);
                    user.DeadlineReset = deadlinereset;
                    var resetcode = CreateResetCode();
                    user.ResetCode = resetcode;
                    if (user.Update(user))
                    {
                        var url = HttpContext.Current.Request.Url;

                        var result = SendEmail("noreplu@dlu.edu.vn", user.Email, "Xác nhận thay đổi mật khẩu",
                            string.Format("Nhấn vào link sau de thay đổi mật khẩu http://{0}:{1}/Account/ResetPassword.aspx?ResetCode={2}", url.Host, url.Port, user.ResetCode));
                        if(result)
                        {
                            return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                        }
                    }
                }

                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        public static string CreateResetCode()
        {
            const string valid = "abcdefghijklmnopXYZ12dkjfalsdkfpdfhfgritytwxyzABCDEFGHIudcnfkgjdfgsla4567890qrstuvJKLMNOPQRSTUVW";
            StringBuilder res = new StringBuilder();
            Random rnd = new Random();
            var length = 45;
            while (0 < length--)
            {
                res.Append(valid[rnd.Next(valid.Length)]);
            }
            return res.ToString();
        }

        public static bool SendEmail(string from, string to, string subject, string body)
        {
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(to);
            mail.From = new MailAddress("hoangthiluong91@gmail.com", "DLU", System.Text.Encoding.UTF8);
            mail.Subject = subject;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = body;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("hoangthiluong91@gmail.com", "mailuong");
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            try
            {
                client.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}